<template>
  <entity-view
    form-item-width="33.33%"
    @submit="submit"
    ref="form"
    :model="beanInfo"
    :rules="rules"
  >
    <div class="sub-content-flex">
      <form-item
        label="单笔最大限额"
        v-model="beanInfo.singleLimitMaxAmt"
        prop="singleLimitMaxAmt"
      ></form-item>
      <form-item
        label="单笔最小限额"
        v-model="beanInfo.singleLimitMinAmt"
        prop="singleLimitMinAmt"
      ></form-item>
      <form-item
        label="日累计限额"
        v-model="beanInfo.dayLimitAmt"
        prop="dayLimitAmt"
      ></form-item>
      <form-item
        label="日累计限次"
        v-model="beanInfo.dayLimitNum"
        prop="dayLimitNum"
      ></form-item>
      <form-item
        label="月累计限额"
        v-model="beanInfo.monthLimitAmt"
        prop="monthLimitAmt"
      ></form-item>
      <form-item
        label="月累计限次"
        v-model="beanInfo.monthLimitNum"
        prop="monthLimitNum"
      ></form-item>
      <form-item
        label="单笔退货限额"
        v-model="beanInfo.singleRefundLimitAmt"
        prop="singleRefundLimitAmt"
      ></form-item>
      <form-item
        label="单日累计退货限额"
        v-model="beanInfo.dayRefundLimitAmt"
        prop="dayRefundLimitAmt"
      ></form-item>
    </div>
    <div class="btn-wrapper">
      <a-button type="primary" html-type="submit" @click="goUpdateOne(preOne)"
        >上一步</a-button
      >
      <a-button
        type="primary"
        html-type="submit"
        @click="goUpdateOne(nextOne)"
        >下一步</a-button
      >
      <a-button @click="close">取消</a-button>
    </div>
  </entity-view>
</template>

<script>
import EntityView from "../../../../components/common/entityView/EntityView";
import FormItem from "../../../../components/common/entityView/FormItem";
import moment from "moment";
moment.locale("zh-cn");

export default {
  name: "LimitInfoForm",
  components: { EntityView, FormItem },
  inject: ["PDstChannelContext", "operationContext"],
  data: function () {
    return {
      pageInfo: {
        mchntType: "",
      },
      beanInfo: {},
      dataSource: [],
      rules: {
        singleLimitMaxAmt: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 15) {
                  callback(new Error("不能超过15位字符"))
                }
                if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                  callback(new Error("保留小数点后两位"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        singleLimitMinAmt:[{
          validator: function(rule,value,callback){
            if (value){
              if (value.length > 15) {
                callback(new Error("不能超过15位字符"))
              }
              if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                callback(new Error("保留小数点后两位"));
              }else{
                callback();
              }}else{
              callback();
            }
          },
          trigger: "blur",
        }],
        dayLimitAmt: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 15) {
                  callback(new Error("不能超过15位字符"))
                }
                if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                  callback(new Error("保留小数点后两位"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        dayLimitNum: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 8) {
                  callback(new Error("不能超过8位字符"))
                }
                if(!(/^[1-9][0-9]{0,7}$/.test(value))){
                  callback(new Error("请输入整数"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          }
        ],
        monthLimitAmt: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 15) {
                  callback(new Error("不能超过15位字符"))
                }
                if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                  callback(new Error("保留小数点后两位"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        monthLimitNum: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 8) {
                  callback(new Error("不能超过8位字符"))
                }
                if(!(/^[1-9][0-9]{0,7}$/.test(value))){
                  callback(new Error("请输入整数"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          }
        ],
        singleRefundLimitAmt: [ {
          validator: function(rule,value,callback){
            if (value){
              if (value.length > 15) {
                callback(new Error("不能超过15位字符"))
              }
              if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                callback(new Error("保留小数点后两位"));
              }else{
                callback();
              }}else{
              callback();
            }
          },
          trigger: "blur",
        },
        ],
        dayRefundLimitAmt: [
          {
            validator: function(rule,value,callback){
              if (value){
                if (value.length > 15) {
                  callback(new Error("不能超过15位字符"))
                }
                if(!(/^\d*(\.\d{1,2})?$/.test(value))){
                  callback(new Error("保留小数点后两位"));
                }else{
                  callback();
                }}else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
      operatorType: "",
      preOne: "timeInfo",
      nextOne: "tradeInfo",
    };
  },
  methods: {
    async getPageInfo() {
      try {
        debugger;
        const res = await this.$get("/admin/mcmp/bTxnLimitCfg/editFromChnl", {
          dstChannelId: this.PDstChannelContext.beanInfo.dstChannelId,
          instId: this.PDstChannelContext.beanInfo.instId,
          optFlag: "upd",
        });
        this.beanInfo = res.data || {};
      } catch (e) {
        console.error(e);
      }
    },
    async  getChannel(){
          try {
            const res = await this.$get("/admin/igas/pDstChannelInfo", {
              instId: this.beanInfo.instId,
              dstChannelId: this.beanInfo.dstChannelId,
              dstChannelName: this.beanInfo.dstChannelName,
            });
            this.dataSource = res.data.rows;
            console.log(this.dataSource);
          } catch (e) {
            console.error(e);
          }
        },
    close(){
      this.PDstChannelContext.modalVisible=false;
    },
    handleChange(value, param) {
      this.beanInfo[param] = value;
    },
    dateChange(value, param) {
      this.beanInfo[param] = moment(value).format("YYYY-MM-DD");
    },
    submit() {
      this.$emit("submit", ...arguments);
    },
    resetFields() {
      return this.$refs.form.resetFields();
    },
    async goUpdateOne(param) {
      if (this.operationContext.operatorType=='view'){
        const res = await this.$refs.form.validate();
        this.operationContext.tabActiveKey = param
      }else {
      const res = await this.$refs.form.validate();
      if (res) {
        this.operationContext.tabActiveKey = param;

        const params = {};
        Object.assign(params, this.beanInfo, {
          id: this.beanInfo.id,
          optFlag: "upd",
          dstChannelId: this.PDstChannelContext.beanInfo.dstChannelId,
          channelTxn: this.dataSource.channelTxn,
        });

        const res = await this.$post("/admin/mcmp/bTxnLimitCfg/edit",params
        );
        if (res.status === 200) {
          this.$message.success("保存成功！");
          return true;
        } else {
          this.$message.error(res.data.message);
          return false;
        }
      }}
    },
  },
  async mounted() {
    await this.getPageInfo();
    await this.getChannel();
    this.operatorType = this.operationContext.operatorType;
  },
};
</script>

<style scoped>
.btn-wrapper {
  padding: 0 10px;
  width: 100%;
  text-align: center;
}
.subtitle {
  width: 100%;
  font-size: 16px;
  margin-bottom: 10px;
  padding: 0 10px;
}

.sub-content-flex {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}

.sub-content-block {
  display: block;
  flex-wrap: wrap;
  width: 100%;
  padding: 0 10px;
}
.checkAll {
  display: block;
  line-height: unset;
}
</style>
